package com.woniuxy.dao;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.controller.info.UserAndAccountInfo;
import com.woniuxy.controller.info.UserAndRolesInfo;
import com.woniuxy.model.RoleModel;
import com.woniuxy.model.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.utils.GetSqlUtil;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 马世亮
 * @since 2022-02-15
 */
public interface UserDao extends BaseMapper<User> {

    @Select("SELECT\n" +
            "\ty.u_id u_id,\n" +
            "\ty.u_name u_name\n" +
            "FROM (SELECT \n" +
            "\ttu.u_id u_id,\n" +
            "\ttu.u_name u_name,\n" +
            "\ttu.account_id account_id,\n" +
            "  u.u_id u_id1\n" +
            "FROM t_user tu\n" +
            "LEFT JOIN (SELECT \n" +
            "\tDISTINCT \n" +
            "\ttu.u_id u_id,\n" +
            "\ttu.u_name u_name\n" +
            "FROM t_user tu\n" +
            "JOIN project_role_user r \n" +
            "WHERE tu.u_id = r.u_id AND r.project_id = #{pid} AND tu.u_delect=0) u \n" +
            "ON u.u_id = tu.u_id\n" +
            ") y\n" +
            "JOIN account_role ar ON ar.account_id = y.account_id \n" +
            "WHERE y.u_id1 is null\n")
    List<User> getUserNorole(@Param("pid") Integer pid);

    @Select("SELECT  tu.u_id u_id,\n" +
            "\t\t\t\ttu.u_name u_name,\n" +
            "\t\t\t\tpr.project_role_id project_role_id,\n" +
            "\t\t\t\tpr.project_role_name project_role_name\n" +
            "FROM t_user tu\n" +
            "JOIN project_role_user pru ON tu.u_id = pru.u_id\n" +
            "JOIN project_role pr ON pr.project_role_id = pru.project_role_id\n" +
            "WHERE pru.project_id =#{pid} AND tu.u_delect= 0 AND pr.project_role_delect=0")
    IPage<UserAndRolesInfo> getUserAndRole(@Param("pid") Integer pid,Page<UserAndRolesInfo> lipage);

    @Select("SELECT * FROM t_user WHERE (u_id =#{info} OR u_name =#{info}) AND u_delect= 0")
    List<User> getUserByinfo(@Param("info")String info);

    @Select("SELECT \n" +
            "\tt.u_id u_id,\n" +
            "\tt.account_id account_id,\n" +
            "\tt.account_username account_username,\n" +
            "\ta.password account_password,\n" +
            "\tt.u_name u_name,\n" +
            "\tt.u_gender u_gender,\n" +
            "\tt.u_tel u_tel,\n" +
            "\tt.u_email u_email,\n" +
            "\tt.project_role_id project_role_id,\n" +
            "\tt.project_ids project_ids,\n" +
            "\tt.u_state u_state,\n" +
            "\trm.role_id role_id,\n" +
            "\trm.role_name role_name\n" +
            "FROM t_user t \n" +
            "JOIN t_account a ON t.account_id = a.a_id\n" +
            "LEFT JOIN account_role ar ON ar.account_id = t.account_id\n" +
            "LEFT JOIN role_model rm ON rm.role_id = ar.role_id WHERE t.u_delect= 0")
    IPage<UserAndAccountInfo> getAllusers(Page<UserAndAccountInfo> page);

    @UpdateProvider(type = GetSqlUtil.class,method = "getIsnotProject")
    Integer updateuser(@Param("s") String s);

    @SelectProvider(type = GetSqlUtil.class,method = "getUidAndRole")
    List<UserAndAccountInfo> getUidAndRoleid(@Param("s") String s);

    @Select("SELECT \n" +
            "\tDISTINCT\n" +
            "\trm.role_id role_id,\n" +
            "\trm.role_name role_name\n" +
            "FROM t_user tu\n" +
            "JOIN account_role ar ON ar.account_id = tu.account_id\n" +
            "JOIN role_model rm ON rm.role_id = ar.role_id\n" +
            "WHERE tu.u_id = #{uid} AND tu.u_delect= 0")
    RoleModel isOrnotManager(Integer uid);


    @Select("SELECT  tu.u_id u_id,\n" +
            "\t\t\t\ttu.u_name u_name,\n" +
            "\t\t\t\tpr.project_role_id project_role_id,\n" +
            "\t\t\t\tpr.project_role_name project_role_name\n" +
            "FROM t_user tu\n" +
            "JOIN project_role_user pru ON tu.u_id = pru.u_id\n" +
            "JOIN project_role pr ON pr.project_role_id = pru.project_role_id\n" +
            "WHERE pru.project_id =#{pid} AND tu.u_delect=0 AND pr.project_role_delect=0")
    List<UserAndRolesInfo> getHasRoleUser(@Param("pid") Integer pid);



}

